package com.maya.service;


import java.util.List;

import org.bson.types.ObjectId;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.maya.dao.QuestionDAO;
import com.maya.model.Question;


@Service
public class QuestionService {
    
	@Autowired
	private QuestionDAO dao ;
	
	public Question get(ObjectId id) {
		
        return dao.get(id);
    }
	
    public void save(Question q) {
        dao.save(q);
    }

	public List<Question> queryByTitle(String searchContent) {
				
        Query<Question> query;
		if(searchContent==null){
        query = dao.createQuery().order("-createdAt");
		}else {
		query = dao.createQuery().field("title").contains(searchContent).order("-createdAt");
		
		}
        List<Question> questions = query.asList();
		return questions;
	}

	public List<Question> find(List<ObjectId> qids) {
		
		Query<Question> query = dao.createQuery().field("_id").hasAnyOf(qids).order("-createdAt");
        List<Question> questions = query.asList();
        return questions;
	}
   
   
}
